﻿using System;
using FlashCards.Dto;
using System.Text;
using System.Windows.Input;
using FlashCards.SL.FlashCardRepeater.ViewModels.Commands;
using System.Collections.Generic;
using FlashCards.SL.FlashCardRepeater.Utils;

namespace FlashCards.SL.FlashCardRepeater.ViewModels
{
    public class FlashcardViewModel : BaseViewModel
    {
        private Flashcard _card;
        private string _mark;

        public FlashcardViewModel(Flashcard card)
        {
            _card = card;
            InitializeCommands();

            GradesList = new List<string> { "Poor", "Average", "Good" };
        }

        public string Text
        {
            get { return Encoding.UTF8.GetString(_card.Term.Data, 0, _card.Term.Data.Length); }
        }

        public string Answer
        {
            get { return Encoding.UTF8.GetString(_card.Definition.Data, 0, _card.Definition.Data.Length); }
        }

        //public int? LastMark
        //{
        //    get { return _card.LastMark; }
        //    set { _card.LastMark = value; }
        //}

        private bool _isAnswerVisible;

        public bool IsAnswerVisible
        {
            get { return _isAnswerVisible; }
            set { _isAnswerVisible = value; OnPropertyChanged("IsAnswerVisible"); }
        }

        public bool IsMarkSelected
        {
            get { return !string.IsNullOrEmpty(_mark); }
        }

        private string _inputedText;
        public string InputedText
        {
            get { return _inputedText; }
            set { _inputedText = value; OnPropertyChanged("InputedText"); }
        }
        public List<string> GradesList { get; set; }

        public event EventHandler<FlashcardClosingEventArgs> FlashcardClosing;

        #region COMMANDS

        public ICommand ShowAnswerCommand { get; private set; }
        public ICommand SetMarkCommand { get; private set; }
        public ICommand NextFlashcardCommand { get; private set; }
        public ICommand ReplaceAccentCharacterCommand { get; private set; }

        void InitializeCommands()
        {
            ShowAnswerCommand = new Command(ShowAnswer);
            SetMarkCommand = new CommandWithParam(SetMark);
            NextFlashcardCommand = new Command(NextFlashcard);
            ReplaceAccentCharacterCommand = new CommandWithParam(ReplaceAccentCharacter);
        }

        void ShowAnswer()
        {
            IsAnswerVisible = true;
        }

        void SetMark(object value)
        {
            if (value is string)
            {
                _mark = value.ToString();
                OnPropertyChanged("IsMarkSelected");
            }
        }

        void NextFlashcard()
        {
            int numericMark = 0;

            if (_mark == "Poor")
                numericMark = 1;
            if (_mark == "Average")
                numericMark = 2;
            if (_mark == "Good")
                numericMark = 3;

            IsAnswerVisible = false;
            FlashcardClosing(this, new FlashcardClosingEventArgs(numericMark, _card));
            _mark = string.Empty;
            InputedText = string.Empty;
        }

        void ReplaceAccentCharacter(object o)
        {
            var accentParams = o as AccentParameters;

            InputedText = InputedText.Remove(accentParams.Index, 1);
            InputedText = InputedText.Insert(accentParams.Index, accentParams.NewCharacter.ToString());
        }

        #endregion
    }

    public class FlashcardClosingEventArgs : EventArgs
    {
        public int Mark { get; set; }
        public Flashcard Flashcard { get; set; }

        public FlashcardClosingEventArgs(int mark, Flashcard flashcard)
        {
            Mark = mark;
            Flashcard = flashcard;
        }
    }
}
